package com.algo.config;

import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Controller;

import java.io.IOException;

/**
 * 定义扫描拦截器
 */
public class MyTypeFilter implements TypeFilter {

    /**
     * 匹配逻辑
     * @param metadataReader
     * @param metadataReaderFactory
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        // 正在校验的类注解相关信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();

        // 正在校验的类类相关信息，比如查看类是否是抽象的，实现的接口，内部类名称等等
        ClassMetadata classMetadata = metadataReader.getClassMetadata();

        // 正在校验的类资源信息，比如类的全路径，文件名称等等
        Resource resource = metadataReader.getResource();

        if (annotationMetadata.getAnnotations().isPresent(Controller.class)) {
            return true;
        }

        return false;
    }
}
